package com.rmt.www.daoImpl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.rmt.www.dao.TraceabilityDaoV2;
import com.rmt.www.vo.TraceabilityV2;

public class TraceabilityDaoV2Impl implements TraceabilityDaoV2 {
	
	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	

	public void deleteTraceabilityByFromIdAndToId(String fromtable, int fromId,
			String totable, int toId) {
		jdbcTemplate.update("delete from traceability_v2 where artifactfromtable=? and artifactfromid=? and artifacttotable=? and artifacttoid=?", 
				new Object[] { fromtable, fromId, totable, toId}, 
				new int[] { java.sql.Types.VARCHAR, java.sql.Types.INTEGER, java.sql.Types.VARCHAR, java.sql.Types.INTEGER});
	}

	public void deleteTraceabilityByTId(int tid) {
		jdbcTemplate.update("delete from traceability_v2 where id=?", new Object[] {tid}, new int[] {java.sql.Types.INTEGER});
	}

	public boolean determinDependencyByFromAndToId(String fromtable,
			int fromId, String totable, int toId) {
		try{
			Object t= this.jdbcTemplate.queryForObject("select * from traceability_v2 where artifactfromtable=? and artifactfromid=? and artifacttotable=? and artifacttoid=?", 
					new Object[]{fromtable, fromId, totable, toId}, new int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER, java.sql.Types.VARCHAR, java.sql.Types.INTEGER}, 
					new TraceabilityV2RowMapper());
			return true;
		}
		catch(Exception e){
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	public List<TraceabilityV2> getTraceabilitiesByFromId(String fromtable,
			int fromid) {
		return this.jdbcTemplate.query("select * from traceability_v2 where artifactfromtable=? and artifactfromid=?", 
				new Object[]{fromtable, fromid}, new int[]{ java.sql.Types.VARCHAR,java.sql.Types.INTEGER}, new TraceabilityV2RowMapper());
	}

	@SuppressWarnings("unchecked")
	public List<TraceabilityV2> getTraceabilitiesByToId(String totable, int toid) {
		return this.jdbcTemplate.query("select * from traceability_v2 where artifacttotable=? and artifacttoid=?", 
				new Object[]{totable, toid}, new int[]{ java.sql.Types.VARCHAR,java.sql.Types.INTEGER}, new TraceabilityV2RowMapper());
	}

	public void insertTraceability(TraceabilityV2 newTraceability) {
		this.jdbcTemplate.update("insert into traceability_v2(artifactfromtable,artifactfromid,artifacttotable,artifacttoid) values(?,?,?,?)",
				new Object[]{newTraceability.getArtifactfromtable(),newTraceability.getArtifactfromid(), newTraceability.getArtifacttotable(),newTraceability.getArtifacttoid()},
				new int[] { java.sql.Types.VARCHAR,java.sql.Types.INTEGER, java.sql.Types.VARCHAR, java.sql.Types.INTEGER});
	}

	public void updateTraceability(TraceabilityV2 newTraceability) {
		this.jdbcTemplate.update("update traceability_v2 set artifactfromtable=?, artifactfromid=?, artifacttotable=?,artifacttoid=? where id=?",
				new Object[]{newTraceability.getArtifactfromtable(),newTraceability.getArtifactfromid(), newTraceability.getArtifacttotable(),newTraceability.getArtifacttoid(), newTraceability.getId()},
				new int[] { java.sql.Types.VARCHAR,java.sql.Types.INTEGER, java.sql.Types.VARCHAR, java.sql.Types.INTEGER,java.sql.Types.INTEGER});
	}

}
